package com.banmao.showgdlocation

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.activity.ComponentActivity
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.alibaba.fastjson2.JSON
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.banmao.showgdlocation.ui.theme.ShowGDLocationTheme

class MainActivity : ComponentActivity() {

    var clickCount: Int = 0

    // 显示经度
    var longitude_TextView: EditText? = null

    // 显示纬度
    var latitude_TextView: EditText? = null

    // 显示定位
    var locate_Button: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()

        setContentView(R.layout.show_location)

        longitude_TextView = findViewById(R.id.longitude_TextView)
        latitude_TextView = findViewById(R.id.latitude_TextView)
        locate_Button = findViewById(R.id.locate_Button)

        Log.i("1", "Main onCreate...")


//        setContent {
//            ShowGDLocationTheme {
//                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
//                    Greeting(
//                        name = "Android",
//                        modifier = Modifier.padding(innerPadding)
//                    )
//                }
//            }
//        }
    }

    fun locate(view: View) {
        Log.i("1", "locate button click...")

        var context = getApplicationContext()
        AMapLocationClient.updatePrivacyShow(context, true, true)
        AMapLocationClient.updatePrivacyAgree(context, true)

        //声明AMapLocationClient类对象
        var mLocationClient: AMapLocationClient? = null;
        //初始化定位
        try {
            mLocationClient = AMapLocationClient(getApplicationContext());
            //初始化定位参数
            var aMapLocationClientOption: AMapLocationClientOption = AMapLocationClientOption();
            //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否返回地址信息（默认返回地址信息）
            aMapLocationClientOption.setNeedAddress(true);
            //设置是否只定位一次,默认为false
            aMapLocationClientOption.setOnceLocation(true);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(aMapLocationClientOption);

            //异步获取定位结果
            var mListener = LocationListener()

            //设置定位回调监听
            mLocationClient.setLocationListener(mListener)

            mLocationClient.startLocation();

        } catch (e: Exception) {
            throw RuntimeException(e);
        } finally {
//            Thread.sleep(10000)
//            if (mLocationClient != null) {
//                mLocationClient.stopLocation()
//            }
        }

    }


}

//@Composable
//fun Greeting(name: String, modifier: Modifier = Modifier) {
//    Text(
//        text = "Hello $name!",
//        modifier = modifier
//    )
//}
//
//@Preview(showBackground = true)
//@Composable
//fun GreetingPreview() {
//    ShowGDLocationTheme {
//        Greeting("Android")
//    }
//}